﻿using System.Reflection;

namespace Leo.Core.Util.Attributes
{
    public static class CommentExtractor
    {
        public static Dictionary<string, object> GetCommentsExtractFromFieldAttributes
            <T>(this T instance, string symbol = "") where T : class, new()
        {      
            Dictionary<string, object> commentsDictionary = new Dictionary<string, object>();
            Type type = instance.GetType();
            PropertyInfo[] properties = type.GetProperties();

            foreach (PropertyInfo property in properties)
            {
                var propertyName = property.Name;
                object propertyValue = property.GetValue(instance);

                var comments = property.GetCustomAttributes<CommentExtractorAttribute>();
                foreach (var comment in comments)
                {
                    commentsDictionary.Add(comment.Name, propertyValue ?? symbol);
                }
            }
            return commentsDictionary;
        }
    }
}
